package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.IamFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.SafetyRiskMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.SafetyRisk;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.SafetyRiskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangsy on 2016/12/13.
 */
public class SafetyRiskServiceImpl implements SafetyRiskService {
    @Autowired
    private SafetyRiskMapper safetyRiskMapper;
    @Autowired
    private IamFileMapper iamFileMapper;

    @Override
    public int deleteByPrimaryKey(String safetyRiskId) {
        return safetyRiskMapper.deleteByPrimaryKey(safetyRiskId);
    }

    @Override
    public int insert(SafetyRisk record) {
        return safetyRiskMapper.insert(record);
    }

    @Override
    public int insertSelective(SafetyRisk record) {
        return safetyRiskMapper.insertSelective(record);
    }

    @Override
    public SafetyRisk selectByPrimaryKey(String safetyRiskId) {
        return safetyRiskMapper.selectByPrimaryKey(safetyRiskId);
    }

    @Override
    public int updateByPrimaryKeySelective(SafetyRisk record) {
        return safetyRiskMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SafetyRisk record) {
        return safetyRiskMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset() );
        //bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        jsonObject.put("total",safetyRiskMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(safetyRiskMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(SafetyRisk record, String uploadFiles, String deletedFiles) {
        //插入数据成功之后，返回的条数
        int resultCount = 0;
        //保存业务数据
        resultCount =  safetyRiskMapper.insertSelective(record);
        //更新的附件不为空的情况
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getSafetyRiskId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        return resultCount;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(SafetyRisk record, String uploadFiles, String deletedFiles) {
        //插入数据成功之后，返回的条数
        int resultCount = 0;
        resultCount += safetyRiskMapper.updateByPrimaryKeySelective(record);
        //更新附件信息
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getSafetyRiskId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        //需要删除的附件信息
        if(deletedFiles!=null && !deletedFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    iamFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return resultCount;
    }
}
